package com.example.demo.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jdk.nashorn.internal.parser.Token;
import lombok.Data;

import java.util.Date;

public class JwtUtils {
    private  static long expire = 36000;
    private static  String secret = "123456789";

    public static  String generateToken(String username)
    {
        long l = System.currentTimeMillis();
        long exp = l + expire;

        // 创建 JwtBuilder
        JwtBuilder jwtBuilder = Jwts.builder().setSubject(username)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, secret);
        // 设置claims
;

        //        jwtBuilder.setClaims(map);
        jwtBuilder.setExpiration(new Date(exp));
        // 生成token
        return jwtBuilder.compact();
    }

    public  static Claims getClaimsByToken(String token)
    {
        return Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();

    }
}
